"""
提取图片/视频的元数据信息
"""
import exifread

photo_path='F:/Temp/ai/photo/IMG_20240418_172901.jpg'

# 读取照片元数据函数
def extract_photo_metadata(photo_path):
    with open(photo_path, 'rb') as image_file:
        tags = exifread.process_file(image_file)

        # 获取拍摄时间
        try:
            datetime_original = tags['EXIF DateTimeOriginal']
            capture_time = datetime_original.values
            print("拍摄时间:", capture_time)
        except KeyError:
            print("无法找到拍摄时间信息")

        # 获取GPS信息（经纬度）
        try:
            gps_latitude_ref = tags['GPS GPSLatitudeRef'].values
            gps_latitude = tags['GPS GPSLatitude'].values
            lat_degrees = gps_latitude[0].num / gps_latitude[0].den
            lat_minutes = gps_latitude[1].num / gps_latitude[1].den
            lat_seconds = gps_latitude[2].num / gps_latitude[2].den
            latitude = lat_degrees + lat_minutes / 60 + lat_seconds / 3600
            if gps_latitude_ref.lower() == 's':
                latitude *= -1

            gps_longitude_ref = tags['GPS GPSLongitudeRef'].values
            gps_longitude = tags['GPS GPSLongitude'].values
            lon_degrees = gps_longitude[0].num / gps_longitude[0].den
            lon_minutes = gps_longitude[1].num / gps_longitude[1].den
            lon_seconds = gps_longitude[2].num / gps_longitude[2].den
            longitude = lon_degrees + lon_minutes / 60 + lon_seconds / 3600
            if gps_longitude_ref.lower() == 'w':
                longitude *= -1

            print("地理位置（经纬度）:", latitude, longitude)
        except KeyError:
            print("无法找到GPS信息")

# 读取图像的元数据
extract_photo_metadata(photo_path)